import unittest
import requests
import json


class OpenpyxlTest(unittest.TestCase):

    def test_login(self):
        session_id = self.login_handle()
        print("获取到的JSESSIONID: ", session_id)
        print("加入到postman中的格式Cookie: JSESSIONID=" + session_id)

    def login_handle(self):
        url = "http://localhost:8080/cms/login"
        j_username = "aaf4ddcf82faaaa7db1614ebdf9620b82cc8f0434bb03245021df256263428487fc9a954ba9dba1874463970e79ab746bbf254e8eaa856bdec951f0568e347197c8bd20ef076b174860340deb621886425670578c3f57d7e9810d02fb42575b0d72170566275b7ea50f0044fe2adf0cba4602fb06df4960b781cf068e14e228c"
        j_password = "b62494365a82c6287b45fb2bc22adbde342bf4820ed341fdc82dca4b995b72f9255b20f49829eeb23d354755cf29d75874ba267bb8790c129b8fa0e8ecbe0a958cc1960391ac506a7ed9e3e941ae3bc7436534f3a3ec67ff727d922a3b1ef765d658f99f34843627278f107c364009ac657408a08977dc1959fd0028ac9027a7"
        res = requests.post(url, data={"j_username": j_username, "j_password": j_password})
        if res.text is None:
            raise Exception("接口返回结果为空！")

        print("接口返回值：", res.text)
        json_resp = res.json()
        if json_resp["code"] != "0":
            raise Exception("接口状态非成功！")

        cookie = requests.utils.dict_from_cookiejar(res.cookies)
        return cookie["JSESSIONID"]



if __name__ == '__main__':
    unittest.main()